<template>
  <div class="wrapper" ref="wrapper">
    <div class="content">
      <slot></slot>
    </div>
  </div>
</template>

<script>
import BScroll from "better-scroll";

export default {
  data() {
    return {
      scroll: null
    };
  },
  props: {
    probeType: {
      type: Number,
      default: 0
    },
    pullUpLoad: {
      type: Boolean,
      default: true
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.scroll = new BScroll(this.$refs.wrapper, {
        click: true,
        probeType: this.probeType,
        scrollY: true,
        pullUpLoad: this.pullUpLoad
      });

      // 监听滚动的位置, 获取位置，实现对回到顶部按钮的展现或者消失
      this.scroll.on("scroll", position => {
        this.$emit("scroll", position);
      });

      // 监听上拉加载更多,在下拉到最底部时会调用此方法
      this.scroll.on("pullingUp", () => {
        this.$emit("pullingUp");
      });
    });
  },
  methods: {
    // 使用BScroll内部的scrollTo方法实现回到顶部
    scrollTo(x, y, time = 600) {
      this.scroll && this.scroll.scrollTo(x, y, time);
    },
    // 实现继续上拉加载更多
    finishPullUp() {
      this.scroll && this.scroll.finishPullUp();
    },
    // 重新计算高度
    refresh() {
      this.scroll && this.scroll.refresh();
    },
    getScrollY() {
      return this.scroll ? this.scroll.y : 0;
    }
  }
};
</script>

<style scoped></style>
